<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getBookList, getBookCategories } from '@/api/book'
import type { Book, BookQueryParams, PageResult } from '@/types/book'
import { ElMessage, ElMessageBox } from 'element-plus'
import { usePagination } from '@/composables/usePagination'

const router = useRouter()

// 查询参数
const queryParams = ref<BookQueryParams>({
  pageNum: 1,
  pageSize: 10,
  category: '',
  keyword: ''
})

// 图书列表
const bookList = ref<Book[]>([])
const categories = ref<string[]>([])
const total = ref(0)

// 分页
const { pageNum, pageSize, onSizeChange, onCurrentChange } = usePagination(() => fetchBooks())

// 获取图书列表
const fetchBooks = async () => {
  try {
    const params = {
      ...queryParams.value,
      pageNum: pageNum.value,
      pageSize: pageSize.value
    }
    const response = await getBookList(params)
    bookList.value = response.data.records
    total.value = response.data.total
  } catch (error) {
    console.error('获取图书列表失败:', error)
    ElMessage.error('获取图书列表失败')
  }
}

// 获取图书分类
const fetchCategories = async () => {
  try {
    const response = await getBookCategories()
    categories.value = response.data
  } catch (error) {
    console.error('获取图书分类失败:', error)
  }
}

// 查看图书详情
const viewDetail = (bookId: number) => {
  router.push({ name: 'bookDetail', params: { id: bookId } })
}

// 搜索图书
const searchBooks = () => {
  pageNum.value = 1
  fetchBooks()
}

// 重置搜索
const resetSearch = () => {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    category: '',
    keyword: ''
  }
  fetchBooks()
}

// 初始化加载数据
onMounted(() => {
  fetchBooks()
  fetchCategories()
})
</script>

<template>
  <div class="book-list-container">
    <el-card>
      <template #header>
        <div class="header">
          <span>图书列表</span>
          <el-button type="primary" @click="fetchBooks">刷新</el-button>
        </div>
      </template>

      <div class="search-form">
        <el-form :inline="true" :model="queryParams">
          <el-form-item label="分类">
            <el-select
              v-model="queryParams.category"
              placeholder="请选择分类"
              clearable
            >
              <el-option
                v-for="category in categories"
                :key="category"
                :label="category"
                :value="category"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="关键词">
            <el-input
              v-model="queryParams.keyword"
              placeholder="请输入关键词"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchBooks">搜索</el-button>
            <el-button @click="resetSearch">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <el-table :data="bookList" border style="width: 100%">
        <el-table-column prop="title" label="书名" width="180" />
        <el-table-column prop="author" label="作者" width="120" />
        <el-table-column prop="category" label="分类" width="120" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag v-if="row.status === 0" type="success">可借阅</el-tag>
            <el-tag v-else type="danger">已借出</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="{ row }">
            <el-button
              type="primary"
              size="small"
              @click="viewDetail(row.bookId)"
            >
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="pageNum"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="onSizeChange"
          @current-change="onCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<style scoped>
.book-list-container {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
